const router = new require('koa-router')()
    , token = require('./config').token
    , checkSignature = require('cheak-wechat-signature-koa')(token)
    , utils = require('./utils')

module.exports = router
.use(checkSignature)
.get( '/', async function (ctx){
    ctx.response.body = ctx.query.echostr
})
.post( '/', async function(ctx){
    const text = await utils.getRawBody(ctx)
    const msg = await utils.xml2json(text)
    const reply = utils.buildMsg(msg.xml)
    ctx.status = 200
    ctx.type = 'application/xml'
    ctx.body = reply
})
